package com.wd.mylibrary_base.base

import android.os.Bundle
import android.os.PersistableBundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import java.lang.reflect.ParameterizedType

/**
 * 作者： 杨辰意
 * 日期： 2024/9/11
 * 时间： 20:21
 * 描述：
 *      _              _           _     _   ____  _             _ _
 *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 * 厚礼谢！
 * ----------------------------------------------------------------
 */
abstract class BaseActivity<VM:ViewModel,D:ViewDataBinding>(private  var layoutId:Int):AppCompatActivity() {


    lateinit var dataBinding:D
    lateinit var viewModel:VM

    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
        dataBinding = DataBindingUtil.setContentView(this,layoutId)
        dataBinding.lifecycleOwner=this

        var type=javaClass.genericSuperclass as ParameterizedType
        viewModel= ViewModelProvider(this)[type.actualTypeArguments[0] as Class<VM>]

        initView()

        initData()

    }

    abstract fun initData()

    abstract fun initView()

    override fun onDestroy() {
        super.onDestroy()
        if (dataBinding!=null) {
            dataBinding.unbind()
        }
    }



}